import VueRouter from "vue-router";
import Vue from "vue";
import Layout from "@/views/layout";
import Login from "@/views/login";
import Dashboard from "@/views/dashboard";
import User from "@/views/users";
import Article from "@/views/article";
import store from "@/store";
import JiaoSe from "@/views/jiaose";
import GoodS from "@/views/goods";
import ParamS from "@/views/params";
import CateGories from "@/views/categories";
import OrderS from "@/views/orders";
import ReoortS from "@/views/reports";
Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    { path: "/login", component: Login },
    {
      path: "/",
      component: Layout,
      redirect: "/article",
      children: [
        { path: "/rights", component: Dashboard },
        { path: "/article", component: Article },
        { path: "/users", component: User },
        { path: "/roles", component: JiaoSe },
        { path: "/goods", component: GoodS },
        { path: "/params", component: ParamS },
        { path: "/categories", component: CateGories },
        { path: "/orders", component: OrderS },
        { path: "/reports", component: ReoortS },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  console.log(token);
  if (to.path !== "/login" && !token) return next("/login");
  next();
});

export default router;
